﻿using ZYCommon;
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using Windows32;

namespace TextEditor
{
   

    public class ImageAreaAction : ImageEditAction
    {
        private double dblContainArea = 0.0;
        private double dblXRate = 0.0;
        private double dblYRate = 0.0;
        public Point DrawFix = Point.Empty;
        private Point[] myPoints = null;

        public double CalcuteArea()
        {
            if (((this.myPoints == null) || (this.myPoints.Length < 3)) || ((this.dblXRate == 0.0) || (this.dblYRate == 0.0)))
            {
                return 0.0;
            }
            double num = 0.0;
            for (int i = 0; i < (this.myPoints.Length - 2); i++)
            {
                double num3 = this.myPoints[0].X * this.myPoints[i].Y;
                num3 += this.myPoints[i].X * this.myPoints[i + 1].Y;
                num3 += this.myPoints[i + 1].X * this.myPoints[0].Y;
                num3 -= this.myPoints[0].X * this.myPoints[i + 1].Y;
                num3 -= this.myPoints[i].X * this.myPoints[0].Y;
                num3 -= this.myPoints[i + 1].X * this.myPoints[i].Y;
                num += num3 / 2.0;
            }
            return Math.Abs((double) (num / (this.dblXRate * this.dblYRate)));
        }

        public Color CalcuteAvgColor(Bitmap myBMP, ProgressHandler vProgress)
        {
            int[] numArray = new int[3];
            if ((this.myPoints != null) && (this.myPoints.Length > 3))
            {
                GraphicsPath path = new GraphicsPath();
                path.AddPolygon(this.myPoints);
                double num = 0.0;
                double num2 = 0.0;
                double num3 = 0.0;
                int num4 = 0;
                Rectangle rectangle = Rectangle.Intersect(RectangleObject.ConvertToRectangle(path.GetBounds()), new Rectangle(0, 0, myBMP.Size.Width, myBMP.Size.Height));
                int right = rectangle.Right;
                int bottom = rectangle.Bottom;
                for (int i = rectangle.Left; i < right; i++)
                {
                    for (int j = rectangle.Top; j < bottom; j++)
                    {
                        if (path.IsVisible(i, j))
                        {
                            Color pixel = myBMP.GetPixel(i, j);
                            num += pixel.R;
                            num2 += pixel.G;
                            num3 += pixel.B;
                            num4++;
                        }
                    }
                    if (vProgress != null)
                    {
                        vProgress(i - rectangle.Left, right - rectangle.Left);
                    }
                }
                if (num4 > 0)
                {
                    return Color.FromArgb((int) (num / ((double) num4)), (int) (num2 / ((double) num4)), (int) (num3 / ((double) num4)));
                }
            }
            return Color.Black;
        }

        public override bool Execute(Graphics g, Rectangle ClipRect)
        {
            if (((this.myPoints != null) && (this.myPoints.Length > 3)) && (g != null))
            {
                int hDC = g.GetHdc().ToInt32();
                int drawMode = Gdi32.GetROP2(hDC);
                int hObject = Gdi32.CreatePen(0, 1, 0xffffff);
                int num4 = Gdi32.SelectObject(hDC, hObject);
                Gdi32.SetROP2(hDC, 7);
                Gdi32.MoveToEx(hDC, this.myPoints[0].X + this.DrawFix.X, this.myPoints[0].Y + this.DrawFix.Y, 0);
                for (int i = 1; i < this.myPoints.Length; i++)
                {
                    Gdi32.LineTo(hDC, this.myPoints[i].X + this.DrawFix.X, this.myPoints[i].Y + this.DrawFix.Y);
                }
                Gdi32.SelectObject(hDC, num4);
                Gdi32.DeleteObject(hObject);
                Gdi32.SetROP2(hDC, drawMode);
                g.ReleaseHdc(new IntPtr(hDC));
                return true;
            }
            return false;
        }

        public override Rectangle Bounds
        {
            get
            {
                return base.myBounds;
            }
            set
            {
            }
        }

        public double ContainArea
        {
            get
            {
                return this.dblContainArea;
            }
        }

        public Point[] Points
        {
            get
            {
                return this.myPoints;
            }
            set
            {
                this.myPoints = value;
                this.dblContainArea = this.CalcuteArea();
                base.myBounds = PointBuffer.GetBounds(this.myPoints);
            }
        }

        public double XRate
        {
            get
            {
                return this.dblXRate;
            }
            set
            {
                this.dblXRate = value;
            }
        }

        public double YRate
        {
            get
            {
                return this.dblYRate;
            }
            set
            {
                this.dblYRate = value;
            }
        }
    }
}

